package ru.ifmo;

import lejos.hardware.Sound;
import sofia_kp.SSAP_sparql_response;
import sofia_kp.iKPIC_subscribeHandler2;

import java.io.*;
import java.util.Vector;

/**
 * Created by Admin on 06.02.15.
 */
public class GrabberListener implements iKPIC_subscribeHandler2 {

    private LegoRobot grabber;
    private String currentColor;

    public GrabberListener(LegoRobot grabber) {
        this.grabber = grabber;
    }

    @Override
    public void kpic_RDFEventHandler(Vector<Vector<String>> added, Vector<Vector<String>> removed, String s, String s1) {
        Sound.beep();
        if (added.size() == 1) {
            SmartSpaceTriplet triplet = new SmartSpaceTriplet(added.elementAt(0));

            if (triplet.getPredicate().equals("hasColor")) {
                currentColor = triplet.getObject();
            }   else if (triplet.getPredicate().equals("hasStatus") && triplet.getSubject().equals("SortingLine")) {
                grabber.subscribeData();
            }  else if (triplet.getPredicate().equals("hasStatus") && triplet.getSubject().equals("Object")) {

                int pathLength = 0;

                if (currentColor.equals("Red")) {
                    pathLength = 60;
                } else if (currentColor.equals("Green")) {
                    pathLength = 40;
                } else if (currentColor.equals("Blue")) {
                    pathLength = 20;
                } else {
                    pathLength = 0;
                }

                try {
                    grabber.startScenario(pathLength);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    public void kpic_SPARQLEventHandler(SSAP_sparql_response ssap_sparql_response, SSAP_sparql_response ssap_sparql_response1, String s, String s1) {

    }

    @Override
    public void kpic_UnsubscribeEventHandler(String s) {

    }

    @Override
    public void kpic_ExceptionEventHandler(Throwable throwable) {

    }
}
